<?php

namespace app\common\model;

use think\Model;

class CouponCollectRecord extends Model
{
    // 表名
    protected $name = 'coupon_collect_record';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'create_time_text',
	    'type_text',
	    'status_text'
    ];
    
	const TYPE_TBK = 1;
	const TYPE_JD = 2;
	const TYPE_PDD = 3;
	
	public static $typeList = [
		self::TYPE_TBK => '淘宝客',
		self::TYPE_JD => '京东',
		self::TYPE_PDD => '拼多多'
	];
	
	const STATUS_N = 0;
	const STATUS_W = 1;
	const STATUS_Y = 2;
	const STATUS_P = 3;
	
	public static $statusList = [
		self::STATUS_W => '匹配出错',
		self::STATUS_N => '待匹配',
		self::STATUS_Y => '返利完成',
		self::STATUS_P => '匹配成功'
	];
    
	public function getTypeTextAttr($value, $data) {
		$value = $value ? $value : $data['type'];
		$list = self::$typeList;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	public function getStatusTextAttr($value, $data) {
		$value = $value ? $value : $data['status'];
		$list = self::$statusList;
		return isset($list[$value]) ? $list[$value] : '';
	}


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

	public function user(){
		return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
	}
}
